package servlet;

import entity.MeetingRoom;
import service.MeetingRoomService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/queryRooms")
public class QueryMeetingRoomServlet extends HttpServlet {
    private MeetingRoomService roomService = new MeetingRoomService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 处理中文乱码
        req.setCharacterEncoding("UTF-8");

        // 2. 获取表单提交的查询关键词
        String queryName = req.getParameter("queryName").trim();

        // 3. 调用Service层查询（若关键词为空则查全部）
        List<MeetingRoom> roomList;
        if (queryName == null || queryName.isEmpty()) {
            roomList = roomService.findAllRooms(); // 查全部
        } else {
            roomList = roomService.findRoomsByName(queryName); // 按名称查
        }

        // 4. 存储查询结果和关键词（回显到页面）
        req.setAttribute("roomList", roomList);
        req.setAttribute("queryName", queryName); // 回显查询框内容

        // 5. 转发到首页显示结果
        req.getRequestDispatcher("/index.jsp").forward(req, resp);
    }
}